﻿using System;
using System.Linq;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class AssociationExpenses : BasePage<AssociationExpensesViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {
                var currentCommittee = this.ViewModel.GetAssociation();
                RefreshPage();
                storeProjects.DataSource = this.ViewModel.GetUserProjects();
                storeProjects.DataBind();
                txtAssociationName.Text = currentCommittee.CommitteeName;
                txtAssociationBalance.Text = this.ViewModel.GetCommitteeBalance().ToString();
            }
        }

        /// <summary>
        /// Handles the Clicked event of the BtnWithdraw control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnWithdraw_Clicked(object sender, AjaxEventArgs e)
        {

            this.ViewModel.WithDrawFromCommittee(Convert.ToDouble(txtWithdrawAmount.Value), Convert.ToDateTime(dateWithdrawDate.Value),
                txtWithdrawCheckNumber.Text, txtNotes.Text, cmbProjects.SelectedItem.Value.ToGuid());
            RefreshPage();
            this.uxExpensesWindow.Hide();
        }

        protected void BtnNewExpenses_Click(object sender, AjaxEventArgs e)
        {
            uxExpensesWindow.Show();
        }

        /// <summary>
        /// Handles the Clicked event of the btnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnBack_Clicked(object sender, AjaxEventArgs args)
        {
            this.RedirectToPreviousPage();
        }

        /// <summary>
        /// Refreshes the page.
        /// </summary>
        private void RefreshPage()
        {
            txtNotes.Reset();
            txtWithdrawAmount.Reset();
            txtWithdrawCheckNumber.Reset();
            dateWithdrawDate.Reset();
            var currentCommittee = this.ViewModel.GetAssociation();
            var expenses = from expense in currentCommittee.CommitteeBankAccounts.SelectMany(commBA => commBA.CommitteeBankAccountExpenses).OrderBy(expen => expen.WithdrawDate)
                           select new
                           {
                               WithdrawDate = expense.WithdrawDate,
                               WithdrawAmount = expense.WithdrawAmount,
                               WithdrawCheckNumber = expense.WithdrawCheckNumber,
                               Notes = expense.Notes,
                               ProjectName = expense.CommitteeBankAccount.Project.ProjectName
                           };
            storeAssociationExpenses.DataSource = expenses;
            storeAssociationExpenses.DataBind();
            double committeeBalance = this.ViewModel.GetCommitteeBalance();
            txtAssociationBalance.Text = committeeBalance.ToString();
            txtWithdrawAmount.MaxValue = committeeBalance > 0 ? committeeBalance : 0;
        }

        public string PermissionName
        {
            get { return Permissions.AssociationExpenses.ToString(); }
        }
    }
}
